Adding some more judges, here and there.
[and.git] / UVa / 562 - Dividing coins / robado2.cpp
blob56eab21cdbecfd6b9bb7ddb2a2279830224c0053
1 //Robado de: http://www.wretch.cc/blog/celiaailec/19559822
2 // Q562: Dividing coins
3 // 2008.05.28 Celia
4 // Method: Dynamic Programming (0-1 knapsack)
5 // Result: Accepted
6 // Time: 0.130
7 #include <stdio.h>
9 #define N 101
10 #define M 25001
12 int iabs(int n)
14 return (n > 0)? n : -n;
16 int imax(int n, int m)
18 return (n > m)? n : m;
21 int a[N], v[N][M];
23 int main()
25 int i, j, t, n, m, sum;
26 scanf("%d", &t);
28 while(t-- > 0)
30 scanf("%d", &n);
32 for(sum = 0, i = 1; i <= n; i++)
33 scanf("%d", &a[i]), sum += a[i];
35 m = sum / 2;
37 for(i = 1; i <= n; i++)
39 for(j = 1; j <= m; j++)
41 v[i][j] = imax(v[i-1][j], v[i][j-1]);
43 if(a[i] <= j)
44 v[i][j] = imax(v[i][j], a[i] + v[i-1][j-a[i]]);
48 printf("%d\n", iabs(sum - v[n][m] * 2));
51 return 0;